package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Path;
import java.util.List;

/**
 * @ClassName: BaseAttrController
 * @Author: WDD
 * @CreateTime: 2022-11-30  02:46
 * @Description:
 */
@Api(tags = "平台属性管理")
@RestController
@RequestMapping("/admin/product/")
public class BaseAttrController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    @ApiOperation("根据分类id获取平台属性")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable("category1Id") Long category1Id, @PathVariable("category2Id") Long category2Id, @PathVariable("category3Id") Long category3Id) {
        List<BaseAttrInfo> attrInfos = baseAttrInfoService.attrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(attrInfos);
    }

    @ApiOperation("添加/修改 平台属性")
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        if (baseAttrInfo.getId() == null) {
            //id为空 保存
            baseAttrInfoService.saveAttrInfo(baseAttrInfo);
        } else {
            //id不为空 修改
            baseAttrInfoService.updateAttrInfo(baseAttrInfo);
        }
        return Result.ok();
    }

    @ApiOperation("根据平台属性ID获取平台属性对象数据")
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId) {
        List<BaseAttrValue> attrValueList = baseAttrValueService.getAttrValueListById(attrId);
        return Result.ok(attrValueList);
    }

}